import { arrayToBar } from "./series.js"
export function initBarOption(list = [], config = {}) {
    let { title, row, column,  } = config
    let xAxis = []
    let series = []
    let legend = []


    list.forEach(obj => {
        xAxis.push(obj[row])
    })

    column.forEach(name => {
        let serie = arrayToBar(list, {
            name: name,
            key: name,
            type: config.type || 'bar'
        })
        legend.push(name)
        series.push(serie)
    })


    return {
        tooltip: {
            trigger: 'axis',
            axisPointer: {            // 坐标轴指示器，坐标轴触发有效
                type: 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
            }
        },
        title: {
            text: title
        },
        xAxis: {
            data: xAxis
        },
        yAxis: {},
        legend:{
            data:legend
        },
        series: series,

    }
}

export function initPieOption(list=[], config = {}) {
    let { title,column } = config
    let series = []
    let map = {}
    list.forEach(obj=>{
        Object.keys(obj).forEach(k=>{
            let value = obj[k]
            if(typeof value === 'number'){
                if(map[k] || map[k] === 0){
                    map[k] += value
                }else{
                    map[k] = 0
                }
            }
        })
    })

    column.forEach(name => {
        series.push({
            value:map[name]/list.length,
            name,
        })
    })
    // console.log('map',map,series);
    return {
        title: {
            text: title,
            left: 'center'
        },
        tooltip: {
            trigger: 'item',
            formatter: '{a} <br/>{b} : {c} ({d}%)'
        },
        legend: {
            // orient: 'vertical',
            // top: 'middle',
            bottom: 10,
            left: 'center',
            data: column
        },
        series: [
            {
                name: 'data',
                type: 'pie',
                radius: '55%',
                data: series,
            },

        ],
    }
}